﻿function LoadCategoryAutoComplete(id) {
    var cacheCategorydata = new Array();
    categorycount = 0;
    //cacheCategorydata["category"]    
    $.ajax({
        type: "POST",
        url: "HandlerSearchCV.ashx",
        data: { 'search': 'categoryById', 'Id': id },
        dataType: "json",
        success: function (data) {

            $("input:text[id='CategoryReadOnlyText']").val(data.toString());
        },

        error: function (response) {
           // alert(response.toString());
        }


    })

    $("a[id='CategoryAdd']").live("click", null, function () {

        categorycount++;
        var txtTagsID = "CategoryTxt" + window.count;
        // var btnAddID = "add" + window.count;
        var btnDelID = "CategoryDel" + window.count;

        if (categorycount == 2)
            $("a[id='CategoryAdd']").hide();
        else
            $("a[id='CategoryAdd']").show();
    
        var ul = $("#divCategoryAdd");
        if (categorycount <= 2) {
            var li = $("<div class=\"col-md-12 add-extra-another-input\"><input id='" + txtTagsID + "'  type=\"text\" placeholder=\"Enter Category\" class=\"form-control\" />"
                 //   + "<button id='" + btnAddID + "' >Add</button>"
                    + "<button id='" + btnDelID + "' class=\"add-extra-another-button\" ><img width=\"16\" height=\"16\" src=\"/img/delete-cross.png\"></button>")
            ul.before(li);
        }
        return false;
    });

    $("button[id^='CategoryDel']").live("click", null, function () {
        var li = $(this).parent();

        $("a[id='CategoryAdd']").show();

        li.remove();
        categorycount--;
        return false;
    });
    
    $("input:text[id^='CategoryTxt']").live("focus.autocomplete", null, function () {
        $(this).autocomplete({
            source: function (request, response) {
                //var loggedIn = Sys.Services.AuthenticationService.get_isLoggedIn();
                if (cacheCategorydata["category"] == null) {
                    $.ajax({
                        type: "POST",
                        url: "HandlerSearchCV.ashx",
                        data: { 'search': 'category' },
                        dataType: "json",
                        async:false,
                        success: function (result) {
                            if (result != undefined) {
                                result.sort();

                                cacheCategorydata["category"] = result;

                                var re = $.ui.autocomplete.escapeRegex(request.term);
                                var matcher = new RegExp("^" + re, "i");
                                var a = $.grep(cacheCategorydata["category"], function (item, index) {
                                    return matcher.test(item);
                                })
                            }

                            if (typeof a === 'undefined') {
                                response("");
                            }
                            else
                                response(a);
                        },
                        error: function (xhr, tStatus, err) {                            
                            if (xhr.status == "401") {
                                alert(xhr.status + " User Authorisation failed.Please login again.");
                                location.reload();
                            }
                        }
                    })
                }
                else {
                    var re = $.ui.autocomplete.escapeRegex(request.term);
                    var matcher = new RegExp("^" + re, "i");
                    var a = $.grep(cacheCategorydata["category"], function (item, index) {
                        return matcher.test(item);
                    });
                    if (typeof a === 'undefined') {
                        response("");
                    }
                    else
                        response(a);
                }

            },
            select: function (event, ui) {
                if (ui.item) {

                    window.scrollTo(0, 0);
                    $("body").ajaxLoader();

                    var pageSize = $('#ctl00_MainContent_ddlPageSize option:selected').val().trim();

                    var jobTitleText = "";
                    $("#JobTitleSearch :text").each(function () {
                        jobTitleText += $(this).val() + "$";
                    });

                    var jobSeekerPlaceText = "";
                    $("#Place :text").each(function () {
                        jobSeekerPlaceText += $(this).val() + "$";
                    });

                    var jobCategoryText = "";
                    $("#Category :text").each(function () {
                        jobCategoryText += $(this).val() + "$";
                    });


                    var jobSeekerEmploymentType = "";
                    $("#EmploymentType :text").each(function () {
                        jobSeekerEmploymentType += $(this).val() + "$";
                    });


                    var jobSeekerQualification = "";
                    $("#Qualification :text").each(function () {
                        jobSeekerQualification += $(this).val() + "$";
                    });

                    var jobSeekerSkills = "";
                    $("#Skills :text").each(function () {
                        jobSeekerSkills += $(this).val() + "$";
                    });

                    var selectedUpdateTime = $('#ctl00_SearchContainer_ddlCvUpdate option:selected').text().trim();
                    var selectedExperience = $('#ctl00_SearchContainer_DropDownListExperience option:selected').text().trim();

                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "SearchCV.aspx/getByCategory",
                        data: JSON.stringify({ 'search': ui.item.value, 'pageSize': pageSize, 'updateTime': selectedUpdateTime, 'experience': selectedExperience, 'jobTitle': jobTitleText, 'location': jobSeekerPlaceText, 'category': jobCategoryText, 'employmentType': jobSeekerEmploymentType, 'qualification': jobSeekerQualification, 'skills': jobSeekerSkills }),
                        datatype: "json",
                        success: function (data) {
                            PrintGrid(data, 1);
                            $("body").ajaxLoaderRemove().delay(3000);
                        },
                        error: function (xhr, tStatus, err) {
                             $("body").ajaxLoaderRemove().delay(3000) ;
                            if (xhr.status == "401") {
                                alert(xhr.status + " User Authorisation failed.Please login again.");
                                location.reload();
                            }
                        },
                        complete: function (xhr, status) {
                            var msg = xhr.responseText;
}
                    })
                }
            },
            minLength: 1,
            delay: 500


        });
        $(this).autocomplete("search");
    });
}